﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FieldTools.Client.Command;
using FieldTools.Client.Model.Base;
using FieldTools.Util;
using FieldTools.Util.Helper;

namespace FieldTools.Client.ViewModel.Window
{
    public class ADBViewModel: NotificationObject, ISingletonDependency
    {
        public ADBViewModel()
        {

        }

        #region 实体

        private string ip = "192.168.";
        /// <summary>
        /// 无线连接时候的ip
        /// </summary>
        public string Ip
        {
            get { return ip; }
            set { ip = value; this.RaisePropertyChanged(nameof(Ip)); }
        }

        private string port = "5555";
        /// <summary>
        /// 无线连接时候的port
        /// </summary>
        public string Port
        {
            get { return port; }
            set { port = value; this.RaisePropertyChanged(nameof(Port)); }
        }

        private string apkPath;
        /// <summary>
        /// apk路径
        /// </summary>
        public string ApkPath
        {
            get { return apkPath; }
            set { apkPath = value; this.RaisePropertyChanged(nameof(ApkPath)); }
        }

        private string adbOrder;
        /// <summary>
        /// apk路径
        /// </summary>
        public string AdbOrder
        {
            get { return adbOrder; }
            set { adbOrder = value; this.RaisePropertyChanged(nameof(AdbOrder)); }
        }


        private List<string> packList;
        /// <summary>
        /// 包名集合
        /// </summary>
        public List<string> PackList
        {
            get { return packList; }
            set { packList = value; this.RaisePropertyChanged(nameof(PackList)); }
        }
        private string packName;

        /// <summary>
        /// 选中的包的名
        /// </summary>
        public string PackName
        {
            get { return packName; }
            set { packName = value; this.RaisePropertyChanged(nameof(PackName)); }
        }

        private List<CheckBoxModel> hostList;
        /// <summary>
        /// host标识集合
        /// </summary>
        public List<CheckBoxModel> HostList
        {
            get { return hostList; }
            set { hostList = value; this.RaisePropertyChanged(nameof(HostList)); }
        }
        private CheckBoxModel selectedHost;
        /// <summary>
        /// 选中的host
        /// </summary>
        public CheckBoxModel SelectedHost
        {
            get { return selectedHost; }
            set { selectedHost = value; this.RaisePropertyChanged(nameof(SelectedHost)); }
        }

        private string outPut;
        /// <summary>
        /// 输出
        /// </summary>
        public string OutPut
        {
            get { return outPut; }
            set { outPut = value; this.RaisePropertyChanged(nameof(OutPut)); }
        }

        private bool isActiveLoading;
        /// <summary>
        /// Loading活跃
        /// </summary>
        public bool IsActiveLoading
        {
            get { return isActiveLoading; }
            set { isActiveLoading = value; this.RaisePropertyChanged(nameof(IsActiveLoading)); }
        }
        #endregion

        #region 事件

        private BaseCommand? getDevices;
        /// <summary>
        /// 获取设备列表
        /// </summary>
        public BaseCommand GetDevices
        {
            get
            {
                if (getDevices == null)
                {
                    getDevices = new BaseCommand(new Action<object>(async o =>
                    {
                        try
                        {
                            IsActiveLoading = true;
                            await Task.Run(() =>
                            {
                                var list = ADBHelper.GetDevices();
                                var oldList = HostList;
                                var hosts = list.Select(x => new CheckBoxModel() { IsChecked = false, Text = x, Status = 0 }).ToList();
                                if (oldList != null && oldList.Any())
                                {
                                    hosts.ForEach(item =>
                                    {
                                        var old = oldList.Find(x => x.Text == item.Text);
                                        if (old != null)
                                        {
                                            item.Text = old.Text;
                                            item.IsChecked = old.IsChecked;
                                            item.Status = old.Status;
                                        }
                                    });
                                }
                                HostList = hosts;
                                IsActiveLoading = false;
                            });
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }));
                }
                return getDevices;
            }
        }

        private BaseCommand? selectAll;
        /// <summary>
        /// 全选设备设备列表
        /// </summary>
        public BaseCommand SelectAll
        {
            get
            {
                if (selectAll == null)
                {
                    selectAll = new BaseCommand(new Action<object>(o =>
                    {
                        try
                        {
                            HostList = HostList.Select(x=>new CheckBoxModel() { IsChecked=true, Text=x.Text, Status=x.Status}).ToList();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }));
                }
                return selectAll;
            }
        }

        private BaseCommand? connectServer;
        /// <summary>
        /// 启动服务
        /// </summary>
        public BaseCommand ConnectServer
        {
            get
            {
                if (connectServer == null)
                {
                    connectServer = new BaseCommand(new Action<object>(async o =>
                    {
                        try
                        {
                            IsActiveLoading = true;
                            await Task.Run(() => {
                                HostList.ForEach(item =>
                                {
                                    if (item.IsChecked && !string.IsNullOrEmpty(item.Text))
                                    {
                                        OutPut = ADBHelper.Connect(item.Text);
                                        item.Status = 1;
                                    }
                                });
                                IsActiveLoading = false;
                            });
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }));
                }
                return connectServer;
            }
        }

        private BaseCommand? disconnectServer;
        /// <summary>
        /// 断开连接
        /// </summary>
        public BaseCommand DisconnectServer
        {
            get
            {
                if (disconnectServer == null)
                {
                    disconnectServer = new BaseCommand(new Action<object>(async o =>
                    {
                        try
                        {
                            IsActiveLoading = true;
                            await Task.Run(() => {    
                                HostList.ForEach(item =>
                                {
                                    if (item.IsChecked && !string.IsNullOrEmpty(item.Text))
                                    {
                                        OutPut = ADBHelper.DisConnect(item.Text);
                                        item.Status = 0;
                                    }
                                });
                                IsActiveLoading = false;
                            });
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }));
                }
                return disconnectServer;
            }
        }

        private BaseCommand? connectNetServer;
        /// <summary>
        /// 连接(网络)
        /// </summary>
        public BaseCommand ConnectNetServer
        {
            get
            {
                if (connectNetServer == null)
                {
                    connectNetServer = new BaseCommand(new Action<object>(async o =>
                    {
                        try
                        {
                            IsActiveLoading = true;
                            await Task.Run(() => {    
                                if (!string.IsNullOrEmpty(Ip) && !string.IsNullOrEmpty(Port))
                                {
                                    OutPut = ADBHelper.Connect(Ip, Port);
                                }
                                IsActiveLoading = false;
                            });
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }));
                }
                return connectNetServer;
            }
        }

        private BaseCommand? startNetServer;
        /// <summary>
        /// 打开网络服务
        /// </summary>
        public BaseCommand StartNetServer
        {
            get
            {
                if (startNetServer == null)
                {
                    startNetServer = new BaseCommand(new Action<object>(async o =>
                    {
                        try
                        {
                            IsActiveLoading = true;
                            await Task.Run(() => {
                                HostList.ForEach(item =>
                                {
                                    if (item.IsChecked && !string.IsNullOrEmpty(item.Text))
                                    {
                                        OutPut = ADBHelper.OpenAdbd(item.Text);
                                        item.Status = 1;
                                    }
                                });
                                IsActiveLoading = false;
                            });
                                
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }));
                }
                return startNetServer;
            }
        }

        private BaseCommand? openFileDialog;
        /// <summary>
        /// 打开文件对话框（apk）
        /// </summary>
        public BaseCommand OpenFileDialog
        {
            get
            {
                if (openFileDialog == null)
                {
                    openFileDialog = new BaseCommand(new Action<object>(o =>
                    {
                        OpenFileDialog open = new()
                        {
                            Filter = "apk|*.apk"
                        };
                        if (open.ShowDialog() == DialogResult.OK)
                        {
                            ApkPath = open.FileName;
                        }
                    }));
                }
                return openFileDialog;
            }
        }

        private BaseCommand? installApk;
        /// <summary>
        /// 安装apk
        /// </summary>
        public BaseCommand InstallApk
        {
            get
            {
                if (installApk == null)
                {
                    installApk = new BaseCommand(new Action<object>(async o =>
                    {
                        try
                        {
                            IsActiveLoading = true;
                            await Task.Run(() => {    
                                HostList.ForEach(item =>
                                {
                                    if (item.IsChecked && !string.IsNullOrEmpty(item.Text))
                                    {
                                        OutPut = string.IsNullOrEmpty(ApkPath) ? "" : ADBHelper.InstallApk(item.Text, ApkPath);
                                    }
                                });
                                IsActiveLoading = false;
                            });
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        
                    }));
                }
                return installApk;
            }
        }

        private BaseCommand? installApkToSysyem;
        /// <summary>
        /// 安装apk
        /// </summary>
        public BaseCommand InstallApkToSysyem
        {
            get
            {
                if (installApkToSysyem == null)
                {
                    installApkToSysyem = new BaseCommand(new Action<object>(async o =>
                    {
                        try
                        {
                            IsActiveLoading = true;
                            await Task.Run(() => {
                                HostList.ForEach(item =>
                                {
                                    if (item.IsChecked && !string.IsNullOrEmpty(item.Text))
                                    {
                                        OutPut = string.IsNullOrEmpty(ApkPath) ? "" : ADBHelper.InstallApkToSystem(item.Text, ApkPath);
                                    }
                                });
                                IsActiveLoading = false;
                            });
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }));
                }
                return installApkToSysyem;
            }
        }
        private BaseCommand? getThirdPackage;
        /// <summary>
        /// 获取包
        /// </summary>
        public BaseCommand GetThirdPackage
        {
            get
            {
                if (getThirdPackage == null)
                {
                    getThirdPackage = new BaseCommand(new Action<object>(async o =>
                    {
                        try
                        {
                            IsActiveLoading = true;
                            await Task.Run(() => {
                                HostList.ForEach(item =>
                                {
                                    if (item.IsChecked && !string.IsNullOrEmpty(item.Text) && SelectedHost!=null && SelectedHost.IsChecked &&! string.IsNullOrEmpty(SelectedHost.Text))
                                    {
                                        PackList = ADBHelper.GetThirdPackage(SelectedHost.Text);
                                    }
                                });
                                IsActiveLoading = false;
                            });
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }));
                }
                return getThirdPackage;
            }
        }

        private BaseCommand? uninstallPack;
        /// <summary>
        /// 卸载包
        /// </summary>
        public BaseCommand UninstallPack
        {
            get
            {
                if (uninstallPack == null)
                {
                    uninstallPack = new BaseCommand(new Action<object>(async o =>
                    {
                        try
                        {
                            IsActiveLoading = true;
                            await Task.Run(() => {
                                HostList.ForEach(item =>
                                {
                                    if (!string.IsNullOrEmpty(PackName) && item.IsChecked && !string.IsNullOrEmpty(item.Text) && SelectedHost != null && SelectedHost.IsChecked && !string.IsNullOrEmpty(SelectedHost.Text))
                                    {
                                        OutPut = ADBHelper.UnInstallPackage(SelectedHost.Text, PackName);
                                    }
                                });
                                IsActiveLoading = false;
                            });
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }));
                }
                return uninstallPack;
            }
        }

        private BaseCommand? closeApp;
        /// <summary>
        /// 关闭app
        /// </summary>
        public BaseCommand CloseApp
        {
            get
            {
                if (closeApp == null)
                {
                    closeApp = new BaseCommand(new Action<object>(async o =>
                    {
                        try
                        {
                            IsActiveLoading = true;
                            await Task.Run(() => {
                                HostList.ForEach(item =>
                                {
                                    if (!string.IsNullOrEmpty(PackName) && item.IsChecked && !string.IsNullOrEmpty(item.Text) && SelectedHost != null && SelectedHost.IsChecked && !string.IsNullOrEmpty(SelectedHost.Text))
                                    {
                                        OutPut = ADBHelper.CloseApp(SelectedHost.Text, PackName);
                                    }
                                });
                                IsActiveLoading = false;
                            });
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }));
                }
                return closeApp;
            }
        }

        private BaseCommand? executeAbdbOrder;
        /// <summary>
        /// 执行adb命令
        /// </summary>
        public BaseCommand ExecuteAbdbOrder
        {
            get
            {
                if (executeAbdbOrder == null)
                {
                    executeAbdbOrder = new BaseCommand(new Action<object>(async o =>
                    {
                        try
                        {
                            IsActiveLoading = true;
                            await Task.Run(() => {
                                HostList.ForEach(item =>
                                {
                                    if (item.IsChecked && !string.IsNullOrEmpty(item.Text))
                                    {
                                        OutPut = string.IsNullOrEmpty(AdbOrder) ? "" : ADBHelper.ExecuteAbdbOrder(item.Text, AdbOrder);
                                    }
                                });
                                IsActiveLoading = false;
                            });
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }));
                }
                return executeAbdbOrder;
            }
        }

        private BaseCommand? restartSystem;
        /// <summary>
        /// 重启系统
        /// </summary>
        public BaseCommand RestartSystem
        {
            get
            {
                if (restartSystem == null)
                {
                    restartSystem = new BaseCommand(new Action<object>(async o =>
                    {
                        try
                        {
                            IsActiveLoading = true;
                            await Task.Run(() => {
                                HostList.ForEach(item =>
                                {
                                    if (item.IsChecked && !string.IsNullOrEmpty(item.Text))
                                    {
                                        OutPut = ADBHelper.RestartSystem(item.Text);
                                    }
                                });
                                IsActiveLoading = false;
                            });
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }));
                }
                return restartSystem;
            }
        }

        #endregion

        #region 方法

        #endregion
    }
}
